package io.lkt.modules.sys.controller;

import com.google.common.collect.Lists;
import io.lkt.common.utils.PageUtils;
import io.lkt.common.utils.R;
import io.lkt.common.validator.ValidatorUtils;
import io.lkt.modules.sys.entity.BTeamRelationEntity;
import io.lkt.modules.sys.entity.BUserInfoEntity;
import io.lkt.modules.sys.entity.SysGzhEntity;
import io.lkt.modules.sys.service.BTeamRelationService;
import io.lkt.modules.sys.service.BUserInfoService;
import io.lkt.modules.sys.service.SysUserRoleService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 团队关系信息表
 *
 * @author xss
 * @email dustwas@163.com
 * @date 2021-02-19 16:11:38
 */
@RestController
@RequestMapping("/sys/bteamrelation")
public class BTeamRelationController {
    @Autowired
    private BTeamRelationService bTeamRelationService;

    @Autowired
    private BUserInfoService bUserInfoService;

    @Autowired
    private SysUserRoleService sysUserRoleService;

    /**
     * 列表
     */
    @RequestMapping("/list")
//    @RequiresPermissions("sys:bteamrelation:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = bTeamRelationService.queryPage(params);

        List<?> list = page.getList();

        if (CollectionUtils.isNotEmpty(list)) {
            List<BTeamRelationEntity> bTeamRelationList = Lists.newArrayList();

            for (Object temp : list) {
                BTeamRelationEntity bTeamRelation = (BTeamRelationEntity) temp;
                // 昵称
                if ((bTeamRelation.getUserId() != null) && (!bTeamRelation.getUserId().equals(""))) {
                    BUserInfoEntity bUserInfo = (BUserInfoEntity) this.bUserInfoService.getById(bTeamRelation.getUserId());
                    bTeamRelation.setNickName(bUserInfo.getNickName());
                    bTeamRelation.setAvatarUrl(bUserInfo.getAvatarUrl());
                }

                //公众号名称
                if ((bTeamRelation.getGzhCode() != null) && (!bTeamRelation.getGzhCode().equals(""))) {
                    SysGzhEntity sysGzh = this.sysUserRoleService.getGzhByCode(bTeamRelation.getGzhCode());
                    bTeamRelation.setGzhName(sysGzh.getGzhName());
                }

                Map<String, Object> t = new HashMap<>();
                t.put("teamId", bTeamRelation.getTeamId());
                bTeamRelation.setTotal(bTeamRelationService.queryTotal(t));

                bTeamRelation.setCreateTime(bTeamRelationService.queryCreateTime(t).getCreateTime());

                bTeamRelationList.add(bTeamRelation);
            }

            page.setList(bTeamRelationList);
        }

        return R.ok().put("page", page);
    }

    @RequestMapping("/listForTeamId")
//    @RequiresPermissions("sys:bteamrelation:list")
    public R listForTeamId(@RequestParam Map<String, Object> params) {
        PageUtils page = bTeamRelationService.queryPageForTeamId(params);

        String teamInfoName = "";
        List<?> list = page.getList();

        if (CollectionUtils.isNotEmpty(list)) {
            List<BTeamRelationEntity> bTeamRelationList = Lists.newArrayList();

            for (Object temp : list) {
                BTeamRelationEntity bTeamRelation = (BTeamRelationEntity) temp;

                teamInfoName = bTeamRelation.getTeamName();
                // 昵称
                if ((bTeamRelation.getUserId() != null) && (!bTeamRelation.getUserId().equals(""))) {
                    BUserInfoEntity bUserInfo = (BUserInfoEntity) this.bUserInfoService.getById(bTeamRelation.getUserId());
                    bTeamRelation.setNickName(bUserInfo.getNickName());
                    bTeamRelation.setAvatarUrl(bUserInfo.getAvatarUrl());
                }

                //公众号名称
                if ((bTeamRelation.getGzhCode() != null) && (!bTeamRelation.getGzhCode().equals(""))) {
                    SysGzhEntity sysGzh = this.sysUserRoleService.getGzhByCode(bTeamRelation.getGzhCode());
                    bTeamRelation.setGzhName(sysGzh.getGzhName());
                }

                Map<String, Object> t = new HashMap<>();
                t.put("teamId", bTeamRelation.getTeamId());
                bTeamRelation.setTotal(bTeamRelationService.queryTotal(t));

                bTeamRelationList.add(bTeamRelation);
            }

            page.setList(bTeamRelationList);
        }

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
//    @RequiresPermissions("sys:bteamrelation:info")
    public R info(@PathVariable("id") Integer id) {
        BTeamRelationEntity bTeamRelation = bTeamRelationService.getById(id);

        return R.ok().put("bTeamRelation", bTeamRelation);
    }

    /**
     * 信息
     */
    @RequestMapping("/infoForTeamId/{teamId}")
//    @RequiresPermissions("sys:bteamrelation:info")
    public R infoForTeamId(@PathVariable("teamId") Integer teamId) {

        Map<String, Object> t = new HashMap<>();
        t.put("teamId", teamId);
        BTeamRelationEntity bTeamRelation = bTeamRelationService.queryCreateTime(t);

        return R.ok().put("teamInfoName", bTeamRelation.getTeamName()).put("teamInfoNum", bTeamRelationService.queryTotal(t));
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
//    @RequiresPermissions("sys:bteamrelation:save")
    public R save(@RequestBody BTeamRelationEntity bTeamRelation) {
        bTeamRelationService.save(bTeamRelation);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
//    @RequiresPermissions("sys:bteamrelation:update")
    public R update(@RequestBody BTeamRelationEntity bTeamRelation) {
        ValidatorUtils.validateEntity(bTeamRelation);
        bTeamRelationService.updateById(bTeamRelation);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
//    @RequiresPermissions("sys:bteamrelation:delete")
    public R delete(@RequestBody Integer[] ids) {
        bTeamRelationService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    @RequestMapping("/getUserTeamCount/{userId}")
    public R getUserTeamCount(@PathVariable("userId") Integer userId) {
        if (userId == null || userId <= 0)
            return R.error("访问失败");
        int userTeamCount = bTeamRelationService.getUserTeamCount(userId);
        return R.ok().put("userTeamCount", userTeamCount);
    }

}
